#!/usr/bin/python3

import glob
import re
import json

generatormap = {}

for filename in glob.glob("intel/*.json"):
    generatorstate = ""
    intelarea = ""
    intellist = []
    filedata = re.match(".*\/LM([0-9]+).json", filename)
    if filedata:
        issueid = filedata.groups(0)[0]
        file = open(filename, "r")
        intel = json.load(file)
        currentflag = not intel["closed"]
        for label in intel["labels"]:
            if label["name"] == "deprecated" or label["name"] == "duplicate" or label["name"] == "documentation" or label["name"] == "bug" or label["name"] == "invalid":
                currentflag = currentflag and False
        if currentflag:
            for line in intel["body"].splitlines():
                matchdata = re.match("^### (.*)", line)
                if matchdata:
                    generatorstate = matchdata.groups(0)[0]
                else:
                    if line != "" and line != "_No response_":
                        if generatorstate == "Area":
                            intelarea = line
                            if intelarea not in generatormap:
                                generatormap[intelarea] = {}
                        elif generatorstate == "Finding":
                            intelreference = line
                            if intelreference not in generatormap[intelarea]:
                                generatormap[intelarea][intelreference] = {}
                            else:
                                print("E: we have a duplicate: %s" % intelreference)
                        else:
                            intellist.append(line)
            generatormap[intelarea][intelreference]["id"] = issueid
            generatormap[intelarea][intelreference]["notes"] = intellist

intelarea = ""
file = open("src/README.md.templ", "r")
for line in file.readlines():
    line = line.strip()
    matchdata = re.match("^#(#+) (.*)", line)
    if matchdata:
        markdownheader = matchdata.groups(0)[0]
        intelarea = matchdata.groups(0)[1]
        print("#%s %s" % (markdownheader, intelarea))
    else:
        matchdata = re.match("^{}", line)
        if matchdata:
            if intelarea in generatormap:
                for intelreference in generatormap[intelarea]:
                    intelnotes = ""
                    for intelnote in generatormap[intelarea][intelreference]["notes"]:
                        intelnote = intelnote.replace("../tree/", "../../tree/")
                        intelnote = intelnote.replace("../blob/", "../../blob/")
                        if intelnotes == "":
                            intelnotes = intelnote
                        else:
                            intelnotes = intelnotes + ", " + intelnote
                    if intelnotes == "":
                        print("* %s ([#%s](https://github.com/timb-machine/linux-malware/issues/%s))" % (intelreference, generatormap[intelarea][intelreference]["id"], generatormap[intelarea][intelreference]["id"]))
                    else:
                        print("* %s ([#%s](https://github.com/timb-machine/linux-malware/issues/%s)) - %s" % (intelreference, generatormap[intelarea][intelreference]["id"], generatormap[intelarea][intelreference]["id"], intelnotes))
                intelarea = ""
        else:
            print(line)
